//制定一个变量
//alert(11)
var statusState = '',
    goBack = '',
    uuid = '';

if(getQueryString('index')){
    var indexData = window.sessionStorage.getItem('indexData');
    if(indexData){
        window.sessionStorage.setItem('index', true)
    }else {
        window.sessionStorage.removeItem('index')
    }
}
if(getQueryString('gonghuo')){
    var gonghuoData = window.sessionStorage.getItem('gonghuoData');
    if(gonghuoData){
        window.sessionStorage.setItem('honghuoHistory', true)
        goBack = JSON.parse(window.sessionStorage.getItem('gonghuoData'))['search'];
    }else {
        window.sessionStorage.removeItem('honghuoHistory')
    }

}

//统计页面的显示隐藏
$('#nav_delivery li').on('click',function(){
    $('#nav_delivery span').removeClass('nav_delivery_Color');
    $(this).find('span').addClass('nav_delivery_Color');
    $('#mian_delivery').children().removeClass('mian_delivery_block');
    $('#mian_delivery').children().eq($(this).index()).addClass('mian_delivery_block')
})
//转调首页
$('#footer_delivery li').eq(0).on('click',function(){
    window.sessionStorage.removeItem('gonghuoData')
    window.sessionStorage.removeItem('honghuoHistory')
    window.location.href="index.html";
})
$('.header-tab a').on('click',function(){
    if(window.sessionStorage.getItem('honghuoHistory')){
        window.sessionStorage.removeItem('history')
        window.sessionStorage.removeItem('indexData')
        window.location.href = 'supply.html?'+goBack;
    }else if(window.sessionStorage.getItem('index')) {
        window.sessionStorage.removeItem('gonghuoData')
        window.sessionStorage.removeItem('honghuoHistory')
        window.location.href="index.html";
    }else if(getQueryString('supermarket')) {
        window.location.href = 'supermarket.html';
    }else if(getQueryString('shop')){
        window.location.href = 'personalStore.html?id=' + uuid;
    }else{
        window.history.back();
    }

})
$('#mian_delivery > div h4 > span').on('click', function () {
    window.location.href = 'personalStore.html?id='+ uuid;
})
$('#footer_delivery li').eq(1).on('click',function(){
    $('#sharePage').css({
        'display':'block'
    })
    $('#sharePage').on('click',function(){
        $('#sharePage').css({
            'display':'none'
        })
    })
})

var urlType = '',
    dataType = '';

$('#footer_delivery li').eq(2).on('click',function(){

    if(statusState == 3){
        $('#footer_delivery li').eq(2).css({
            'background':'#ccc'
        })
//		$('#footer_delivery li').eq(2).html('洽谈中');
//		$('#footer_delivery li').eq(2).html('洽谈中')
    }else{
        DetLogin(fn);
    }
    //判断他是否有uuid

})
function fn(){

    urlType = "/order/add/withProvide"
    //这里调用18接口，请球成功跳转页面
    ImporInfor()
    dataType ="provideInfoId="+getQueryString('id') +"&" + Infornation;
    askPostData1(urlType);
}

//初始化页面 首页的信息
//2.1.37
urlType = "/home/getProvideInfoDetail";


dataType ="provideInfoId="+getQueryString('id');
//	请求供货详情列表     get请求方式
askGetData(urlType)
function askGetData(num){
    $.ajax({
        url:host + num,
        type:'post',
        data:dataType,
        success:function(res){
            if(res.success == true){

                var str = '';
                statusState = res.data.status;
                if(statusState == 3){
                    $('#footer_delivery li').eq(2).css({
                        'background':'#ccc'
                    })
                    $('#footer_delivery li').eq(2).html('洽谈中');
                }
                if(res.data.meatCategory.length>=1){
                    for (var i = 0; i < res.data.meatCategory.length; i++) {
                        str +=  res.data.meatCategory[i]+' ';
                    }
                    $('#header_delivery_2 p').html(str)
                }else{
                    $('#header_delivery_2 p').html('暂无信息')
                }
                createLi(res.data)
                contactInfo(res.data);
                createImg(res);
                $('body').show();
                share(res.data.meatCategory,res.data.provideDesc,res.data.imageList[0]);




                // 获取店铺状态
                $.ajax({
                    url: host + '/shop/getUserShop',
                    data:'uuid='+uuid,
                    success:function (data) {
                        if(data.data.status == 2 || data.data.status == 3){
                            $('#shop').removeClass('hidden')
                        }
                    }
                })
            }else{
                //无数据则返回
            }
        }
    })
}
//生成信息详情
var amount = '',//数量
    warehouse = '',//货源地
    country = '',//产地
    amount = '',//数量
    price = '',//价格
    unit = '',//单位
    warehouse = '',//货源地
    meatCategory = '',//供货类型
    packing = '', //包装规格
    scale = '', //精修比例
    updateDate = '',//保质期
    factoryNo = '',//厂号
    company = '',//企业
    provideWay = '',
    shelfLife = '',
    imageList = '',//图片的路径
    status = '';



function createLi(num){
    amount = num.amount;//数量
    warehouse = num.warehouse;//货源地
    country = num.country;//产地
    amount = num.amount;//数量
    price = num.price;//价格
    unit = num.unit;//单位
    warehouse = num.warehouse;//货源地
    meatCategory = num.meatCategory;//供货类型
    packing = num.packing; //包装规格
    scale = num.scale;//精修比例
    updateDate = num.updateDate;//保质期
    factoryNo = num.factoryNo;//厂号
    company = num.company;
    imageList = num.imageList;
    provideWay = num.provideWay;
    shelfLife = num.shelfLife;
    uuid = num.userId;
    var strTimeShow = '';
    price = (price !='' ? price+"/吨" : '议定');
    scale = scale?scale+'%':'';
    warehouse = (warehouse == ''? '' : warehouse);
    // if(status !=1){
    //     $('#shop').removeClass('hidden')
    // }
    //生成数据
    $('#header_delivery_2 strong').html(time(updateDate))
    $('#header_delivery_2 span').html('￥'+ price)
    $('#header_delivery_2').append($('<strong>'+time(updateDate)+'</strong>'));

    //详细的用户列表
//	$('#mian_delivery ul').append($("<h4>产品参数</h4><li>数量:&nbsp;<span>"+ Math.round(amount/1000*100)/100 +"</span></li><li>单位:&nbsp;<span>吨</span></li><li>精修比例:&nbsp;<span>"+ scale +"</span></li><li>产地:&nbsp;<span>"+ country +"</span></li><li>包装规格:&nbsp;<span>"+ packing +"</span></li><li>厂号/品牌:&nbsp;<span>"+ factoryNo +"</span></li><li>保质期:&nbsp;<span>"+ shelfLife.substring(0,10) +"</span></li><li>企业:&nbsp;<span>"+ company +"</span></li>"));
    $('#mian_delivery ul li span').eq(0).html(Math.round(amount/1000*100)/100 +'&nbsp吨');
    $('#mian_delivery ul li span').eq(1).html(factoryNo);
    $('#mian_delivery ul li span').eq(2).html(country);
    $('#mian_delivery ul li span').eq(3).html(packing);
    $('#mian_delivery ul li span').eq(4).html(warehouse);
    $('#mian_delivery ul li span').eq(5).html(provideWay);
    $('#mian_delivery ul li span').eq(6).html(shelfLife.substring(0,10));
    $('#mian_delivery ul li span').eq(7).html(scale);
    $('#mian_delivery ul li span').eq(8).html(company);
}

//生成资源详情信息图
function createImg(res){

    $('#mian_delivery>ol').append('<li><h4>资源详情</h4><p>'+ res.data.provideDesc +'</p></li>')
//	$('#mian_delivery>ol')
    for (var i = 0; i < res.data.imageList.length; i++) {

        if(res.data.imageList.length>=1){
            $('#mian_delivery>ol').append('<li><img src='+ res.data.imageList[i] +'></li>')
            $('#header_delivery img').attr('src',res.data.imageList[0])
        }else{
            $('#mian_delivery>ol').append('<li><img src='+ img/defaultImg.png +'></li>')
        }
    }
}

function contactInfo(num){
    contact = num.contact;//发布人
    phone = num.phone;//
    $('#mian_delivery .active2 em').eq(0).html(contact);
    $('#mian_delivery .active2 em').eq(1).html(phone);
    //添加电话号码的点击事件
    if(phone != ''){
        $('#mian_delivery .active3 a').attr("href",'tel:'+ phone);
    }
}

function askPostData1(num){
    $.ajax({
        url:host + num,
        type:"post",
        data:dataType,
        success:function(res){
            if(res.success == true){
                $('#orderPage').css({
                    'display':'block'
                });
                sureButton();
                //生成数据时页面清空、
//				window.location.href = 'wantToBuyPopup.html';
//				return;
            }else{
                alert(res.message)
//					console.log('laiba1')
                //就是无数据则返回什么
            }
        }
    });
}
function sureButton(){
    $('#orderPage button').on('click',function(){
        if(getQueryString('gonghuo')) {
            window.location.href = 'supply.html?'+goBack;
        }else {
            window.location.href = 'index.html';
        }

    })
}




function share(meatCategory,desc,img){
    var titleShare = "出售-"+(meatCategory+"").substring(0,6)+(meatCategory.length>5?"..":"")+" 找牛网清真牛羊肉交易平台";
    if(img==null||img=="undefined"){
        img='http://m.zhaoniuw.com/img/icon_share.jpg';
    }
    $.ajax({
        url:wechathost+shareurl + shareparm,
        type:'post',
        success:function(res){

            var json = eval('(' + res + ')');
            wx.config({
                debug: false, // 开启调试模￿调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印￿
                appId: 'wx554a3c93d558ae03', // 必填，公众号的唯一标识
                timestamp:json.data.timestamp , // 必填，生成签名的时间￿
                nonceStr: json.data.nonceStr, // 必填，生成签名的随机￿
                signature: json.data.signature,// 必填，签名，见附￿
                jsApiList: [
                    'onMenuShareTimeline',
                    'onMenuShareAppMessage'


                ] // 必填，鿀要使用的JS接口列表，所有JS接口列表见附￿
            });

            wx.ready(function(){


                wx.checkJsApi({
                    jsApiList: ['onMenuShareAppMessage'], // 需要检测的JS接口列表，所有JS接口列表见附￿
                    success: function(res) {


                        // 以键值对的形式返回，可用的api值true，不可用为false
                        // 如：{"checkResult":{"chooseImage":true},"errMsg":"checkJsApi:ok"}
                    }
                });
                wx.onMenuShareTimeline({
                    title: titleShare, // 分享标题
                    link:shareUrl,
                    imgUrl: img, // 分享图标
                    success: function () {

                    },
                    cancel: function () {
                        // 用户取消分享后执行的回调函数
                    }
                });

                wx.onMenuShareAppMessage({
                    title:titleShare, // 分享标题
                    desc: desc, // 分享描述
                    link: shareUrl, // 分享链接
                    imgUrl: img, // 分享图标
                    type: '', // 分享类型,music、video或link，不填默认为link
                    dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空
                    success: function () {
                    },
                    cancel: function () {
                        // 用户取消分享后执行的回调?ǽ敿
                    }
                });


            });

            wx.error(function(res){

            });


        }
    });
}

